%% asm script
%
%%
clear all
clc
close all
%%
folder = '../img/'
imgname ='face00';
imgtype ='.pgm';
imgarray =[];

ut=1

simplenum=500

for i=1:simplenum
	if i<10
		fullname=[folder,imgname,'00',int2str(i),imgtype];
	elseif i<100
		fullname=[folder,imgname,'0',int2str(i),imgtype];
	else
		fullname=[folder,imgname,int2str(i),imgtype];
	end
	img=imread(fullname);
	[row,col]=size(img);
	imgvector=reshape(img,row*col,1);
	imgarray=[imgarray,imgvector];

end


Meanimgarray=repmat(mean(imgarray,2),1,simplenum);

Meanface=reshape(mean(imgarray,2),row,col);

Meanface=uint8(Meanface);

figure(1);
imshow(Meanface);

Mn=double(imgarray)-Meanimgarray;

[U,S,V]=svd(Mn);

B=zeros(row*col,simplenum);

sumSi=0;

SumS=sum(sum(S));

for i=1:size(S,1)

	sumSi=S(i,i)+sumSi;	
	if sumSi >=ut*SumS
                break;
        end
end
diagnum1=i
B=U(:,1:i)*S(1:i,1:i)*V(:,1:i)';

imgnew=B+Meanimgarray;

vec1=imgnew(:,1);
face1=reshape(vec1,row,col);
face1=uint8(face1);
figure(2)
imshow(face1)
